//
//  showPictureView.m
//  tabBarText
//
//  Created by tsb1119 on 2018/1/3.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "showPictureView.h"
#import "UIShortTapGestureRecognizer.h"
@interface showPictureView ()<UIScrollViewDelegate>
@property(assign,nonatomic)CGRect fromFrame;
@property(strong,nonatomic)UIScrollView * imageScrollView;
@property(strong,nonatomic)UIImageView * imageView;
@property(strong,nonatomic)UIPageControl * pageControl;
@property(strong,nonatomic)UIScrollView * scrollView;
@property(strong,nonatomic)NSArray * images;
@property(strong,nonatomic)NSMutableArray * fromFrames;
@end
@implementation showPictureView
-(instancetype)init
{
    if (self = [super initWithFrame:[UIScreen mainScreen].bounds]) {
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:1];
    }
    return self;
}
-(UIScrollView *)scrollView
{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH + 20, SCREEN_HEIGHT)];
        _scrollView.contentSize = CGSizeMake((SCREEN_WIDTH+20) * (self.images.count), 0);
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.pagingEnabled = YES;
        _scrollView.tag = 666;
        _scrollView.delegate = self;
        for (int i = 0; i<self.images.count; i++) {
            UIScrollView * imageviewScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(i*(SCREEN_WIDTH+20), 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
            imageviewScrollView.minimumZoomScale = 1;
            imageviewScrollView.maximumZoomScale = 2;
            imageviewScrollView.delegate = self;
            imageviewScrollView.showsVerticalScrollIndicator=  NO;
            imageviewScrollView.showsHorizontalScrollIndicator = NO;
            //点击
            UIShortTapGestureRecognizer * singleTap = [[UIShortTapGestureRecognizer alloc]initWithTarget:self action:@selector(dismiss)];
            [imageviewScrollView addGestureRecognizer:singleTap];
            //双击
            UIShortTapGestureRecognizer *doubleTap = [[UIShortTapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];
            doubleTap.numberOfTapsRequired = 2;
            [imageviewScrollView addGestureRecognizer:doubleTap];
            //区别单双击
            [singleTap requireGestureRecognizerToFail:doubleTap];
            
            [_scrollView addSubview:imageviewScrollView];
            
            UIImage * image =  self.images[i];
            CGFloat scale = image.size.width/image.size.height;
            CGFloat imageViewH  = SCREEN_WIDTH / scale;
            UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, (SCREEN_HEIGHT-imageViewH)/2, SCREEN_WIDTH, imageViewH)];
            imageView.tag = 555;
            imageView.image = image;
            
            [imageviewScrollView addSubview:imageView];
        }
    }
    return _scrollView;
}
-(UIPageControl *)pageControl
{
    if (!_pageControl) {
        _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT-60, SCREEN_WIDTH, 20)];
        _pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
        _pageControl.pageIndicatorTintColor = [UIColor darkGrayColor];
        _pageControl.numberOfPages = self.images.count;
        _pageControl.hidden = self.images.count == 1;
    }
    return _pageControl;
}
-(void)showImages:(NSArray *)images FromFrames:(NSMutableArray *)fromFrames  index:(NSInteger)index;//传入需要展现的图片 和 图片的相对全屏frame
{
    self.images = images;
    self.fromFrames = fromFrames;
    self.pageControl.currentPage = index;
    UIImage * currentImage = images[self.pageControl.currentPage];
    self.scrollView.contentOffset = CGPointMake((SCREEN_WIDTH+20) * self.pageControl.currentPage, 0);
    
    self.imageScrollView = [[UIScrollView alloc]init];
    self.imageView = [[UIImageView alloc]initWithImage:currentImage];
    self.fromFrame = [fromFrames[self.pageControl.currentPage] CGRectValue];
    self.imageScrollView.frame = self.fromFrame;
    self.imageView.frame = self.imageScrollView.bounds;
    self.imageView.contentMode = UIViewContentModeScaleAspectFill;
    self.imageView.clipsToBounds = YES;
    [self.imageScrollView addSubview:self.imageView];
    
    self.backgroundColor = [UIColor colorWithWhite:0 alpha:1];
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [[UIApplication sharedApplication].keyWindow addSubview:self.imageScrollView];
    [[UIApplication sharedApplication].keyWindow addSubview:self.pageControl];
    [UIView animateWithDuration:0.3 animations:^{
        CGFloat scale = currentImage.size.width/currentImage.size.height;
        CGFloat imageViewH  = SCREEN_WIDTH / scale;
        self.imageScrollView.frame = CGRectMake(0, (SCREEN_HEIGHT-imageViewH)/2, SCREEN_WIDTH, imageViewH);
        self.imageView.frame = self.imageScrollView.bounds;
    } completion:^(BOOL finished) {
        [self addSubview:self.scrollView];
        self.imageScrollView.hidden = YES;
    }];
}
-(void)dismiss
{
    //调整返回的图片
    self.imageView.image = self.images[self.pageControl.currentPage];
    //调整返回的初始位置
    CGFloat scale = self.imageView.image.size.width/self.imageView.image.size.height;
    UIScrollView * imageScrollView = self.scrollView.subviews[self.pageControl.currentPage];
    CGFloat imageViewW = SCREEN_WIDTH * imageScrollView.zoomScale;
    CGFloat imageViewH = imageViewW / scale;
    self.imageScrollView.frame = CGRectMake(0, imageViewH<=SCREEN_HEIGHT?(SCREEN_HEIGHT-imageViewH)/2:0, imageViewW, imageViewH);
    self.imageView.frame = self.imageScrollView.bounds;
    self.imageScrollView.contentOffset = imageScrollView.contentOffset;
    //调整返回的最终位置
    self.fromFrame = [self.fromFrames[self.pageControl.currentPage] CGRectValue];
    
    self.imageScrollView.hidden = NO;
    [self.pageControl removeFromSuperview];
    [self.scrollView removeFromSuperview];
    [UIView animateWithDuration:0.3 animations:^{
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0];
        self.imageScrollView.frame = self.fromFrame;
        self.imageView.frame =self.imageScrollView.bounds;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        [self.imageScrollView removeFromSuperview];
    }];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.tag == 666) {
        self.pageControl.currentPage = (NSInteger)(scrollView.contentOffset.x /(SCREEN_WIDTH+20));
        if (self.didSelected) {
            self.didSelected(self.pageControl.currentPage);
        }
    }
}
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return (UIImageView *)[scrollView viewWithTag:555];
}
-(void)scrollViewDidZoom:(UIScrollView *)scrollView
{
    UIImageView * imageView = (UIImageView *)[scrollView viewWithTag:555];
    imageView.center = CGPointMake(scrollView.contentSize.width/2, scrollView.contentSize.height<SCREEN_HEIGHT?SCREEN_HEIGHT/2: scrollView.contentSize.height/2);
}
- (void)doubleTap:(UITapGestureRecognizer *)gesture
{
    UIScrollView * imageScrollView  = (UIScrollView *)gesture.view;
    if (imageScrollView.zoomScale >1.0) {
        [imageScrollView setZoomScale:1.0 animated:YES];
    }
    else
    {
        CGPoint location = [gesture locationInView:imageScrollView];
        [imageScrollView zoomToRect:CGRectMake(location.x, location.y, 1, 1) animated:YES];
    }
}
@end
